package com.woniuxy.intellihome.service.impl;

import com.woniuxy.intellihome.entity.GoodsSpu;
import com.woniuxy.intellihome.entity.Sku;
import com.woniuxy.intellihome.mapper.GoodsSpuMapper;
import com.woniuxy.intellihome.mapper.SkuMapper;
import com.woniuxy.intellihome.service.GoodsSpuService;
import com.woniuxy.intellihome.service.SkuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.intellihome.service.SpecDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-08-11
 */
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {
    @Autowired
    private GoodsSpuService spuService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private SpecDetailService detailService;
    @Autowired
    private GoodsSpuMapper spuMapper;
    @Autowired
    private SkuMapper skuMapper;


    /**
     * 根据sku的id获取完整sku对象
     * 1、获取sku对象
     * 2、对其赋值SPU对象
     * 3、对齐赋值所属specDetail的list
     *
     * @param id sku的id
     * @return sku对象(含)
     */
    @Override
    public Sku getWithSpuSpecDetailBySKUId(int id) {
        Sku sku = skuMapper.selectById(id);
        sku.setSpu(spuMapper.getSpuBelong2Sku(id));
        sku.setDetails(detailService.getDetailsBelong2Sku(id));
        return sku;
    }

    /**
     * 得到详尽的所属spu的sku列表
     *
     * @param spuId spu的id
     * @return 所属spu的sku列表
     */
    @Override
    public List<Sku> getAllSkuBelong2Spu(int spuId) {
        List<Sku> skus = skuMapper.getAllSkuBelong2Spu(spuId);
        List<Sku> skuList = new ArrayList<>();
        if (skus == null) {
            return null;
        }
        for (Sku sku : skus) {
            Sku skuTemp = skuService.getWithSpuSpecDetailBySKUId(sku.getId());
            skuList.add(skuTemp);
        }
        return skuList;
    }

    /**
     * 得到最新商品，并带出sku明细
     */
    @Override
    public List<Sku> getNew() {
        List<GoodsSpu> goods = spuMapper.getNew();
        if (goods == null) {
            return null;
        }
        List<Sku> skus = new ArrayList<>();
        for (GoodsSpu good : goods) {
            List<Sku> sku = skuService.getAllSkuBelong2Spu(good.getId());
            if (sku != null && !sku.isEmpty()) {
                skus.add(sku.get(0));
            }
        }
        return skus;
    }

    /**
     * 得到最新商品，并带出sku明细
     */
    @Override
    public List<Sku> getHot() {
        List<GoodsSpu> goods = spuMapper.getHot();
        if (goods == null) {
            return null;
        }
        List<Sku> skus = new ArrayList<>();
        for (GoodsSpu good : goods) {
            List<Sku> sku = skuService.getAllSkuBelong2Spu(good.getId());
            if (sku != null && !sku.isEmpty()) {
                skus.add(sku.get(0));
            }
        }
        return skus;
    }

    @Override
    public BigDecimal getPriceById(int id) {
        return skuMapper.getPriceById(id);
    }

    @Override
    public int getSalesById(int id) {
        return skuMapper.getSalesById(id);
    }
}
